home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / Content Creation Tool Plug-Ins / Maya / Sources / PreviewPipeline.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  12.2 KB  |  401 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: PreviewPipeline.h
  3. //
  4. // Defines the interfaces for the preview pipeline
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. //--------------------------------------------------------------------------------------
  8. #pragma once
  9. #ifndef PREVIEWPIPELINE_H
  10. #define PREVIEWPIPELINE_H
  11.  
  12. #include "dxcc.h"
  13.  
  14. #include <vector>
  15. #include <windows.h>
  16. #include <assert.h>
  17. #include <stdio.h>
  18. #include <memory.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. #include <stdlib.h>
  22. #include <math.h>
  23. #include <malloc.h>
  24. #include <atlcoll.h>
  25.  
  26.  
  27.  
  28. // Forward declaration
  29. class CPipelineLock;
  30. class CPipelineEngineEvents;
  31. class CPipelineManagerEvents;
  32. class CPipelineViewerEvents;
  33. class CPipelineEngineEventsTrigger;
  34. class CPipelineManagerEventsTrigger;
  35. class CPipelineViewerEventsTrigger;
  36. class CPipeline;
  37. class CPipelineEngine;
  38. class CPipelineViewer;
  39.  
  40. #define GetSingleObject(WaitForLock, obj) (WAIT_OBJECT_0 == ((WaitForLock) ? WaitForSingleObject(obj, INFINITE) :   WaitForSingleObject(obj, 0)))
  41.  
  42.  
  43. class CPipelineEngineEvents
  44. {
  45. public:
  46.  
  47.     virtual HRESULT OnD3DDeviceCreate(){ return S_OK; };//on Adapter Change recovery
  48.     virtual HRESULT OnD3DDeviceReset(){ return S_OK; };//on Device Lost recovery
  49.     virtual HRESULT OnD3DDeviceLost(){ return S_OK; };//on Device Lost
  50.     virtual HRESULT OnD3DDeviceDestroy(){ return S_OK; };//on Adapter Change
  51. };
  52.  
  53. class CPipelineManagerEvents
  54. {
  55. public:
  56.     CPipelineManagerEvents();
  57.     ~CPipelineManagerEvents();
  58.  
  59.     virtual HRESULT OnReload(){ return S_OK; };//expect that everything is new
  60.     virtual HRESULT OnResourceAdd(IDXCCResource* pRes){ return S_OK; };
  61.     virtual HRESULT OnResourceRecycle(IDXCCResource* pRes){ return S_OK; };
  62.     virtual HRESULT OnResourceUpdate(IDXCCResource* pRes){ return S_OK; };
  63.     virtual HRESULT OnResourceRemove(IDXCCResource* pRes){ return S_OK; };
  64.  
  65. private:
  66.     HANDLE ExclusiveModeMutex;
  67. };
  68.  
  69. class CPipelineViewerEvents
  70. {
  71. public:
  72.  
  73.     virtual HRESULT OnFrameChildAdded(IDXCCFrame* pParent, IDXCCFrame* pChild) { return S_OK; };
  74.     virtual HRESULT OnFrameChildRemoved(IDXCCFrame* pParent, IDXCCFrame* pChild) { return S_OK; };
  75.  
  76.     virtual HRESULT OnFrameMemberAdded(IDXCCFrame* pParent, IUnknown* pChild) { return S_OK; };
  77.     virtual HRESULT OnFrameMemberRemoved(IDXCCFrame* pParent, IUnknown* pChild) { return S_OK; };
  78.  
  79.     virtual HRESULT OnMeshChange(IDXCCMesh* pMesh){ return S_OK; };
  80.  
  81.     virtual HRESULT OnMeshDeclarationChange(IDXCCMesh* pMesh, IDXCCMesh* pOldMesh){ return S_OK; };
  82.     //topology has change, regenerate mesh
  83.     virtual HRESULT OnMeshTopologyChange(IDXCCMesh* pMesh, IDXCCMesh* pOldMesh){ return S_OK; };
  84.     //vertices has changed so updates that range with the given Usage info
  85.     virtual HRESULT OnMeshVertexChange(IDXCCMesh* pMesh, UINT vertMin, UINT vertMax){ return S_OK; };
  86.     virtual HRESULT OnMeshSubVertexChange(IDXCCMesh* pMesh, D3DDECLUSAGE Usage, UINT UsageIndex, UINT vertMin, UINT vertMax){ return S_OK; };
  87.     //the attributes used to associate a material to the mesh have changed
  88.     virtual HRESULT OnMeshAttributeChange(IDXCCMesh* pMesh, UINT faceMin, UINT faceMax){ return S_OK; };
  89.     //the materials associated to the attributes have changed
  90.     virtual HRESULT OnMeshMaterialChange(IDXCCMesh* pMesh, DXCCATTRIBUTE Attrib, ID3DXEffect* pOldMaterial){ return S_OK; };
  91.     //The material's Effect interface has changed (commonly used to unbind from Standard Semantics detabase)
  92.     virtual HRESULT OnMaterialEffectChange(LPD3DXEFFECT pMaterial, LPD3DXEFFECT pOldEffect){ return S_OK; };
  93.     //The material's Effect interface has changed (commonly used to unbind from Standard Semantics detabase)
  94.     virtual HRESULT OnMaterialParameterChange(LPD3DXEFFECT pMaterial, D3DXHANDLE hParameter){ return S_OK; };
  95. };
  96.  
  97. /*
  98. enum
  99. {
  100.     DXCCMSG_GLOBAL_START,
  101.     DXCCMSG_GLOBAL_PAUSE,
  102.     DXCCMSG_GLOBAL_STOP,
  103.     DXCCMSG_DEVICE_CREATE,
  104.     DXCCMSG_DEVICE_RESET,
  105.     DXCCMSG_DEVICE_LOST,
  106.     DXCCMSG_MANAGER_ADD,
  107.     DXCCMSG_MANAGER_REMOVE,
  108.     DXCCMSG_MANAGER_REFRESH,
  109. }
  110.  
  111. class CPipelineCallbacks
  112. {
  113.     void RegisterMessageToHwnd(HWND , WPARAM );
  114.     void UnregisterMessageToHwnd(HWND , WPARAM);
  115.  
  116.     void RegisterMessageToThread(DWORD, WPARAM);
  117.     void UnregisterMessageToThread(DWORD, WPARAM);
  118.  
  119.     //get the count of all threads and hwnds which have registered to recieve this message
  120.     UINT GetDeliveryCount(WPARAM Msg);
  121.  
  122.     //This will post to the Window Message Queue of all registered recipients of WPARAM 
  123.     //the DXCCMSG with WPARAM Msg and LPARAM Package
  124.     //this will Addref #recipients so that the buffer is delete once everyone has recieved it
  125.     //TODO change IDXCCHeap to inherit from ID3DXBuffer
  126.     void PostMessage(WPARAM Msg, LPD3DXBUFFER Package);
  127.     void SendMessage(WPARAM Msg);//COMMANDS ONLY like PAUSE or STOP
  128.  
  129.     //RecievePackage transfers the reference count to the user.
  130.     //When all recipients recieve and release the package, 
  131.     //the package should delete itself if the sender has also released it
  132.     HRESULT RecievePackage(LPARAM, LPD3DXBUFFER* Package);
  133.  
  134. private:
  135.     CAtlMap< WPARAM ,CAtlArray<HWND> > AddressBookOfHwnds;
  136.     CAtlMap< WPARAM ,CAtlArray<DWORD> > AddressBookOfThreads;
  137.     CAtlMap< LPARAM, LPD3DXBUFFER > Packages;
  138.     HANDLE    OwnershipMutex; //all functions will acquire and release this.
  139.  
  140. }
  141. */
  142.  
  143.  
  144. class CPipelineEngineEventsTrigger : public CPipelineEngineEvents
  145. {
  146.     std::vector<CPipelineEngineEvents*> Events;
  147. public:
  148.  
  149.     HRESULT RegisterEvents(CPipelineEngineEvents* pCall);
  150.     HRESULT UnregisterEvents(CPipelineEngineEvents* pCall);
  151.  
  152.  
  153.     HRESULT OnD3DDeviceCreate();
  154.     HRESULT OnD3DDeviceReset();
  155.     HRESULT OnD3DDeviceLost();
  156.     HRESULT OnD3DDeviceDestroy();
  157. };
  158.  
  159. class CPipelineManagerEventsTrigger : public CPipelineManagerEvents
  160. {
  161.     std::vector<CPipelineManagerEvents*> Events;
  162. public:
  163.  
  164.     HRESULT RegisterEvents(CPipelineManagerEvents* pCall);
  165.     HRESULT UnregisterEvents(CPipelineManagerEvents* pCall);
  166.  
  167.  
  168.     HRESULT OnReload(); 
  169.     HRESULT OnResourceAdd(IDXCCResource* pRes);//EMITTED
  170.     HRESULT OnResourceRecycle(IDXCCResource* pRes);//EMITTED
  171.     HRESULT OnResourceRemove(IDXCCResource* pRes);
  172. };
  173.  
  174. class CPipelineViewerEventsTrigger : public CPipelineViewerEvents
  175. {
  176.     std::vector<CPipelineViewerEvents*> Events;
  177. public:
  178.     
  179.     HRESULT RegisterEvents(CPipelineViewerEvents* pCall);
  180.     HRESULT UnregisterEvents(CPipelineViewerEvents* pCall);
  181.  
  182.  
  183.     HRESULT OnFrameChildAdded(IDXCCFrame* pParent, IDXCCFrame* pChild);//EMITTED
  184.     HRESULT OnFrameChildRemoved(IDXCCFrame* pParent, IDXCCFrame* pChild) ;//EMITTED
  185.     HRESULT OnFrameMemberAdded(IDXCCFrame* pParent, IUnknown* pChild);//EMITTED
  186.     HRESULT OnFrameMemberRemoved(IDXCCFrame* pParent, IUnknown* pChild);//EMITTED
  187.     HRESULT OnMeshChange(IDXCCMesh* pMesh);//EMITTED
  188.     HRESULT OnMeshDeclarationChange(IDXCCMesh* pMesh, IDXCCMesh* pOldMesh);
  189.     HRESULT OnMeshTopologyChange(IDXCCMesh* pMesh, IDXCCMesh* pOldMesh);
  190.     HRESULT OnMeshVertexChange(IDXCCMesh* pMesh, UINT vertMin, UINT vertMax);
  191.     HRESULT OnMeshSubVertexChange(IDXCCMesh* pMesh, D3DDECLUSAGE Usage, UINT UsageIndex, UINT vertMin, UINT vertMax);
  192.     HRESULT OnMeshAttributeChange(IDXCCMesh* pMesh, UINT faceMin, UINT faceMax);
  193.     HRESULT OnMeshMaterialChange(IDXCCMesh* pMesh, DXCCATTRIBUTE Attrib, LPD3DXEFFECT pOldMaterial);
  194.     HRESULT OnMaterialEffectChange(LPD3DXEFFECT pMaterial, LPD3DXEFFECT pOldEffect);
  195.     HRESULT OnMaterialParameterChange(LPD3DXEFFECT pMaterial, D3DXHANDLE hParameter);
  196. };
  197.  
  198.  
  199. class CPipeline
  200. {
  201.     friend CPipelineLock;
  202. public:
  203.  
  204.     virtual HRESULT Create();
  205.     virtual HRESULT Destroy();
  206.  
  207.     virtual bool IsValid();
  208.  
  209.     virtual CPipelineEngine* AccessEngine();
  210.     virtual LPDXCCMANAGER AccessManager();
  211.     virtual LPDXCCFRAME AccessRoot();
  212.  
  213.     virtual float GetTime();
  214.     virtual void SetTime(float time);
  215.  
  216.  
  217.     virtual HRESULT SetEngine( CPipelineEngine* pPreviewEngine );
  218.  
  219.  
  220.     virtual bool SceneWriteLock(BOOL WaitForLock, CPipelineLock& Lock); //returns true if you got the lock
  221.     virtual void SceneWriteUnlock(CPipelineLock& Lock);
  222.  
  223.     virtual bool SceneReadLock(BOOL WaitForLock, CPipelineLock& Lock); //returns true if you got the lock
  224.     virtual void SceneReadUnlock(CPipelineLock& Lock);
  225.  
  226.     CPipelineEngineEventsTrigger        TriggerDeviceEvents;
  227.     CPipelineManagerEventsTrigger        TriggerManagerEvents;
  228.     CPipelineViewerEventsTrigger        TriggerViewerEvents;
  229.  
  230. private:
  231.  
  232.  
  233.  
  234. //-------------------------------------------------------------------------------------
  235.     
  236. /* TODO Make device launcher part of PPCore
  237.     void CreateDevice()
  238.         {
  239.  
  240.             // Launch the thread which will create the device and check for device state changes
  241.             m_hThread = _beginthreadex( NULL, 0, StaticRunThread, this, 0, &m_nThreadID );
  242.             if( m_hThread == NULL )
  243.                 return E_FAIL;
  244.  
  245.             GetSingleObject(true, DeviceCreatedEvent);
  246.  
  247.             return S_OK;
  248.  
  249.  
  250.         }
  251.  
  252.         static UINT __stdcall LaunchDeviceThread( void* pParam )
  253.         {
  254.             ((CPipelineEngine*)pParam)->OnThreadInit();
  255.             ((CPipelineEngine*)pParam)->OnThreadRun();
  256.         }
  257.  
  258.  
  259.         UINT __stdcall CEngine::StaticRunThread( void* pParam )
  260.         {
  261.             CEngine* pDevice = (CEngine*) pParam;
  262.             return pDevice->RunThread();
  263.         }
  264.  
  265. */
  266.  
  267.  
  268.  
  269.  
  270.     //everyone must gain the Access to play fair
  271.     //Write not release until unlock so that no reads come though
  272.     //Read releases immediately to support multiple writes
  273.     //Read incriments ReadCount on gain of AccessMutex and decriment on finishing
  274.     //Read sets ZeroEvent when  ReadCount is zero and resets when non-zero
  275.     //write waits on ZeroEvent after it has gained AccessMutex to ensure that reads are finished
  276.     HANDLE    ReadWriteMutex;
  277.     CRITICAL_SECTION ZeroSceneReadersSection; 
  278.     UINT    SceneReadersCount;
  279.     HANDLE    ZeroSceneReadersEvent;    
  280.     
  281.     CPipelineEngine* pPreviewEngine;
  282.     LPDXCCMANAGER pDXCCManager;    
  283.     LPDXCCFRAME pDXCCRoot;
  284.     float fTime;
  285.  
  286.  
  287.  
  288. };
  289.  
  290.  
  291. //we create a CPipelineEngine in it's own thread but you don't have to
  292. //this prevents abnormal crashing caused by living in the same thread as OpenGL
  293. class CPipelineEngine
  294. {
  295. public:
  296.     virtual HRESULT Create(CPipeline* pPreviewPipeline) = 0;
  297.     virtual HRESULT Destroy() = 0;
  298.  
  299.     virtual HRESULT GetD3DObject( IDirect3D9** ppObject ) = 0;
  300.     virtual HRESULT GetD3DDevice( IDirect3DDevice9** ppDevice ) = 0;
  301. };
  302.  
  303.  
  304.  
  305. //it is suggested that the viewers use StateBlocks to ensure that multiple viewers do not collide
  306. class CPipelineViewer 
  307. {
  308. public:
  309.     //get the device from the pPreviewPipeline and use CreateAdditionalSwapChain
  310.     virtual HRESULT Create( CPipeline* pPreviewPipeline,    
  311.                             D3DPRESENT_PARAMETERS* pPresentationParameters,
  312.                             const WCHAR* strWindowTitle = L"Direct3D Preview Pipeline", 
  313.                             HINSTANCE hInstance = NULL, 
  314.                             HICON hIcon = NULL, 
  315.                             HMENU hMenu = NULL,
  316.                             int width = 640,
  317.                             int height = 480,
  318.                             int x = CW_USEDEFAULT, 
  319.                             int y = CW_USEDEFAULT) = 0;
  320.  
  321.     virtual HRESULT Destroy() = 0; //destroy thread
  322.  
  323.     virtual UINT Run() = 0; //allow core logic to start/continue
  324.     virtual UINT GetPauseCount() = 0; //is the core logic running?
  325.     virtual UINT Pause() = 0; //pause the core logic
  326.     
  327.     virtual HWND    GetRenderWindow() = 0; //get window this view will render to
  328.     virtual HWND    GetShellWindow() = 0;//get the highest-level window you'd like docked inside a DCC
  329.     virtual HRESULT    GetD3DSwapChain(IDirect3DSwapChain9** ppSwapChain) = 0;
  330. };
  331.  
  332. class CPipelineLock
  333. {
  334.     friend CPipeline;
  335.     friend CPipelineManagerEvents;
  336. public:
  337.     CPipelineLock(bool AutoUnlock= true);
  338.     ~CPipelineLock();
  339.  
  340.     long ofType(){ return Type; }
  341.     bool isLocked(){ return Locked; }
  342.  
  343. private:
  344.     enum
  345.     {
  346.         TYPE_NONE= 0,
  347.         TYPE_SCENE_WRITE,
  348.         TYPE_SCENE_READ,
  349.         NUMBER_OF_TYPES
  350.     }                    Type;
  351.     bool                Locked;
  352.     bool                UnlockOnDestuction;
  353.     CPipeline*    pPreviewPipeline;
  354. };
  355.  
  356.  
  357.  
  358.  
  359. class CBank
  360. {
  361. public:
  362.     typedef DWORD GroupId;
  363.     typedef DWORD ItemId;
  364.     typedef CRBMap<ItemId , void* > CItemBank;
  365.     typedef CRBMap<ItemId , GroupId > CItemToGroup;
  366.     typedef CRBMultiMap<GroupId, ItemId> CGroupBank;
  367.  
  368.     CItemBank            ItemBank;
  369.     CItemToGroup        ItemToGroup;
  370.     CGroupBank            GroupBank;
  371.  
  372.     CBank();
  373.  
  374.     GroupId GenerateGroupId();
  375.     CItemBank::CPair* AddItemToGroup(GroupId Gid, void* pValue);
  376.  
  377.     void RemoveByItem( ItemId Iid , bool DeleteValue);
  378.     void RemoveByGroup( GroupId Gid, bool DeleteValues );
  379.  
  380.     void RemoveAll(bool DeleteValues);
  381.  
  382.  
  383. protected:
  384.     CBank(CBank& copyMe){};
  385.     operator = (CBank& copyMe){};
  386.  
  387.  
  388.     DWORD GroupGenerator;
  389.     DWORD ItemGenerator;
  390.     
  391. };
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. #endif //PREVIEWPIPELINE_H